package com.hornsun.client.personalPage.service;

import com.google.common.base.Preconditions;
import com.hornsun.domain.constant.types.CouponType;
import com.hornsun.domain.dbo.Coupon;
import com.hornsun.domain.dbo.MemberCoupon;
import com.hornsun.client.personalPage.dto.MemberCouponDTO;
import com.hornsun.repository.CouponRepository;
import com.hornsun.repository.MemberCouponRepository;
import com.hornsun.repository.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by 编译中... on 2018/3/19.
 */
@Service
public class CouponService {
    @Resource
    private MemberCouponRepository memberCouponRepository;



    /**
     * 获取会员的优惠券信息
     * @param openid
     * @param page
     * @param type
     * @return
     */
    public MemberCouponDTO getMemberCoupon(String openid,int page,int type){
        Preconditions.checkNotNull(openid);
        MemberCouponDTO memberCouponDTO = new MemberCouponDTO();
        memberCouponDTO.setUsableCount((int)memberCouponRepository.usableCouponCount(openid));
        memberCouponDTO.setUsedCount((int)memberCouponRepository.UsedCouponCount(openid));
        memberCouponDTO.setOverdueCount((int)memberCouponRepository.OverdueCouponCount(openid));
        if (type== CouponType.USABLE.getType()){
            memberCouponDTO.setCouponList(memberCouponRepository.queryUsableCoupon(openid,page));
        }
        if (type== CouponType.USED.getType()){
            memberCouponDTO.setCouponList(memberCouponRepository.queryUsedCoupon(openid,page));
        }
        if (type== CouponType.OVERDUE.getType()){
            memberCouponDTO.setCouponList(memberCouponRepository.queryOverdueCoupon(openid,page));
        }
        return memberCouponDTO;
    }



    /**
     * 删除无效的优惠券（过期半年以上的）
     */
    public void deleteInvalidMemberCoupon(){
        memberCouponRepository.deleteInvalidMemberCoupon();
    }

}
